using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;


public class UIBuyConfirmPop : UIBasePanel
{

    public Item itemCfg;
    public int BuyOrSell;
    public int MaxCount;
    public int SelectCount;
    public Button Btn_Confirm;
    public Text Txt_Confirm;
    public InputField Input_Confirm;
    public Text Txt_Title;
    public ItemData ItemData;
    // Start is called before the first frame update
    void Start()
    {
        if (UIInfo.opd is BuyOrSellComfirmData)
        {
            BuyOrSellComfirmData opd = (BuyOrSellComfirmData)UIInfo.opd;
            itemCfg = opd.Cfg;
            BuyOrSell = opd.BuyOrSell;
            MaxCount = opd.Count;
        }
        else
        {
            Debug.LogError(UIInfo.UIName + "面板传进来的类型不对");
            return;
        }
        Btn_Confirm = GameObject.Find("Btn_Confirm").GetComponent<Button>();
        Input_Confirm = transform.Find("Input_Confirm").GetComponent<InputField>();
        Txt_Title = transform.Find("Txt_Title").GetComponent<Text>();
        InitUI();
    }

    // Update is called once per frame
    void Update()
    {

    }
    void OnDestroy()
    {

    }


    public void InitUI()
    {
        if (BuyOrSell == (int)BuySellType.Buy)
        {
            BuyUI();
        }
        else
        {
            SellUI();
        }
        Input_Confirm.onValueChanged.AddListener(InputCount);
    }



    public void BuyUI()
    {
        Btn_Confirm.onClick.AddListener(() =>
                   {
                       Logic.BuyItem(this, ItemData);
                   });
        Input_Confirm.transform.GetChild(0).GetComponent<Text>().text = "请输入购买数量";
        Txt_Title.text = "购买";
    }

    public void SellUI()
    {
        Btn_Confirm.onClick.AddListener(() =>
                 {
                     Logic.SellItem(this, ItemData);
                 });
        Input_Confirm.transform.GetChild(0).GetComponent<Text>().text = "请输入出售数量";
        Txt_Title.text = "出售";
    }

    public void InputCount(string arg0)
    {
        SelectCount = int.Parse(arg0);
        ItemData = new ItemData(itemCfg, SelectCount);
    }

}

public struct BuyOrSellComfirmData
{
    public Item Cfg;
    //buy是1，sell是2；
    public int BuyOrSell;
    //可买或可卖的数量
    public int Count;

}

enum BuySellType
{
    Buy = 1,
    Sell = 2,
}